//
//  BZImageKit.m
//  BZBundleKit
//
//  Created by quheng on 2017/9/20.
//  Copyright © 2017年 quheng. All rights reserved.
//

#import "BZImageKit.h"
#import "BZResourcesCache.h"


@interface BZImageKit()


@end

@implementation BZImageKit


+ (UIImage *)imageWithName:(NSString *)imageName
                      type:(NSString *)type
                  inBundle:(NSString *)bundlePath
               inDirectory:(NSString *)directory{
    
    
    NSParameterAssert(imageName);
    NSParameterAssert(type);
    
    NSString *path = nil;
    if (![bundlePath length]) {
        path = [NSBundle mainBundle].bundlePath;
    }else{
        path = bundlePath;
    }
    path = [NSString stringWithFormat:@"%@/Contents/Resources",path];
    path = [directory length]?[path stringByAppendingPathComponent:directory]:path;
    NSUInteger alpha = [UIScreen mainScreen].scale;
    NSString *imageRealPath = nil;
    if (alpha == 1) {
        imageRealPath = [NSString stringWithFormat:@"%@/%@.%@",path,imageName,type];
    }else{
        imageRealPath = [NSString stringWithFormat:@"%@/%@@%lux.%@",path,imageName,(unsigned long)alpha,type];
    }
    
    NSCache *resourceCache = [[BZResourcesCache shareInstance] getResourcesCache];
    if ([resourceCache objectForKey:imageRealPath]) {
        return [resourceCache objectForKey:imageRealPath];
    }else{
        UIImage *image = [UIImage imageWithContentsOfFile:imageRealPath];
        if (image) {
            [resourceCache setObject:image forKey:imageRealPath];
        }
        return image;
    }
    
}

+ (UIImage *)imageWithName:(NSString *)imageName
                      type:(NSString *)type
                bundleName:(NSString *)bundleName
               inDirectory:(NSString *)directory
{
    NSParameterAssert(imageName);
    NSParameterAssert(type);
    
    NSString *path = nil;
    if (![bundleName length]) {
        path = [NSBundle mainBundle].bundlePath;
    }else{
        path = [[NSBundle mainBundle]pathForResource:bundleName ofType:@"bundle"];;
    }
    path = [NSString stringWithFormat:@"%@/Contents/Resources",path];
    path = [directory length]?[path stringByAppendingPathComponent:directory]:path;
    NSUInteger alpha = [UIScreen mainScreen].scale;
    NSString *imageRealPath = nil;
    if (alpha == 1) {
        imageRealPath = [NSString stringWithFormat:@"%@/%@.%@",path,imageName,type];
    }else{
        imageRealPath = [NSString stringWithFormat:@"%@/%@@%lux.%@",path,imageName,(unsigned long)alpha,type];
    }
    
    NSCache *resourceCache = [[BZResourcesCache shareInstance] getResourcesCache];
    if ([resourceCache objectForKey:imageRealPath]) {
        return [resourceCache objectForKey:imageRealPath];
    }else{
        UIImage *image = [UIImage imageWithContentsOfFile:imageRealPath];
        if (image) {
            [resourceCache setObject:image forKey:imageRealPath];
        }
        return image;
    }

}



- (UIImage *)imageWithName:(NSString *)imageName
                      type:(NSString *)type{

    NSParameterAssert(imageName);
    NSParameterAssert(type);
    NSParameterAssert(self.bundlePath);
    
    
    NSUInteger alpha = [UIScreen mainScreen].scale;
    NSString *imageRealPath = nil;
    if (alpha == 1) {
        imageRealPath = [NSString stringWithFormat:@"%@/%@.%@",self.bundlePath,imageName,type];
    }else{
        imageRealPath = [NSString stringWithFormat:@"%@/%@@%lux.%@",self.bundlePath,imageName,(unsigned long)alpha,type];
    }
    
    NSCache *resourceCache = [[BZResourcesCache shareInstance] getResourcesCache];
    
    if ([resourceCache objectForKey:imageRealPath]) {
        return [resourceCache objectForKey:imageRealPath];
    }else{
        UIImage *image = [UIImage imageWithContentsOfFile:imageRealPath];
        [resourceCache setObject:image forKey:imageRealPath];
        return image;
    }
    
    return nil;


}

@end
